package com.hunt.controller;

import com.hunt.dao.TransactionCalendarMapper;
import com.hunt.model.entity.LongHuPoResult;
import com.hunt.model.entity.StockHotInvest;
import com.hunt.service.HotMoneyService;
import com.hunt.service.LongHuService;
import com.hunt.util.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.regex.Matcher;
import java.util.regex.Pattern;


/**
 * @Author sjs
 * @Date 2020/11/13
 * @Description
 */
@Api(value = "热钱/游资接口")
@Controller
@RequestMapping(path = "/hotmoney")
public class HotMoneyController {
    @Autowired
    private HotMoneyService hotMoneyService;
    private static final Pattern HUMP_PATTERN = Pattern.compile("[A-Z0-9]");
    @ApiOperation(value = "查询最新机构投资评级", httpMethod = "GET", produces = "application/json", response = Result.class)
    @ResponseBody
    @RequestMapping(value = "getStockHotInvest", method = RequestMethod.GET)
    public Result getStockHotInvest(@RequestParam(value = "sort",defaultValue = "1")@ApiParam(value = "0为从小到大，1为从大到小")String sort,@RequestParam(value = "position",defaultValue = "0") String position,@RequestParam(value = "size",defaultValue = "10") String size ) {
        if ("0".equals(sort)){
            sort="asc";
        }else {
            sort="desc";
        }
      return  Result.success(hotMoneyService.getStockHotInvest( sort,position,size));
    }
    @ApiOperation(value = "查询游资每日", httpMethod = "GET", produces = "application/json", response = Result.class)
    @ResponseBody
    @RequestMapping(value = "getStockHotDaily", method = RequestMethod.GET)
    public Result getStockHotDaily(@RequestParam(value = "position",defaultValue = "1") String position,@RequestParam(value = "size",defaultValue = "10") String size,@RequestParam(value = "orderfield",defaultValue = "netBuying") String orderfield,@RequestParam(value = "sort",defaultValue = "1")@ApiParam(value = "0为从小到大，1为从大到小")String sort) {
        if ("0".equals(sort)){
            sort="asc";
        }else {
            sort="desc";
        }
        return  Result.success(hotMoneyService.getStockHotDaily(position,size,  humpToLine(orderfield),sort));
    }
    @ApiOperation(value = "查询游资图表", httpMethod = "GET", produces = "application/json", response = Result.class)
    @ResponseBody
    @RequestMapping(value = "getStockHotTu", method = RequestMethod.GET)
    public Result getStockHotTu() {
        return  Result.success(hotMoneyService.getStockHotTu());
    }
    @ApiOperation(value = "查询个股统计", httpMethod = "GET", produces = "application/json", response = Result.class)
    @ResponseBody
    @RequestMapping(value = "getStockHotRecently", method = RequestMethod.GET)
        public Result getStockHotRecently(@RequestParam(value = "type",defaultValue = "1") @ApiParam(value = "1对应5,2对应15,3对应最近一个月，4对应最近3个月，5对应最近12个月")String type,@RequestParam(value = "position",defaultValue = "0") String position,@RequestParam(value = "size",defaultValue = "10") String size,@RequestParam(value = "orderfield",defaultValue = "capitalNumber") String orderfield,@RequestParam(value = "sort",defaultValue = "1")@ApiParam(value = "0为从小到大，1为从大到小")String sort ) {
        if ("0".equals(sort)){
            sort="asc";
        }else {
            sort="desc";
        }
        return  Result.success(hotMoneyService.getStockHotRecently( type,position,size,  humpToLine(orderfield),sort));
    }
    @ApiOperation(value = "返回股票评级网页" , httpMethod = "GET",produces = "text/html")
    @RequestMapping(value = "getStockRankPage", method = RequestMethod.GET)
    public String getStockRankPage(@RequestParam(value = "id") String id,@RequestParam(value = "color",defaultValue = "19212A") String color, Model model) {
        StockHotInvest shi=hotMoneyService.getOneStockHotInvest(id);
        model.addAttribute("title",shi.getTitle());
        model.addAttribute("color",color);
        model.addAttribute("summary_date",shi.getSummaryDate());
        model.addAttribute("core_points",shi.getCorePoints().replaceAll("\\s{2,}","<br><br>    "));
        return "pc/stockrankpage";
    }
    private String humpToLine(String str) {
        Matcher matcher = HUMP_PATTERN.matcher(str);
        StringBuffer sb = new StringBuffer();
        while (matcher.find()) {
            matcher.appendReplacement(sb, "_" + matcher.group(0).toLowerCase());
        }
        matcher.appendTail(sb);
        return sb.toString();
    }

}
